<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: interceptors/content.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: interceptors/content.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>import { is, isNoDataMethod } from '../utils';

const DEFAULT_TYPE = 'application/x-www-form-urlencoded; charset=utf-8';
const JSON_TYPE = 'application/json; charset=utf-8';


/**
 * 通用拦截器之contentInteceptor
 */
const contentInterceptor = {
  fulfill: (request) => {
    const data = request.data;
    const header = request.header;
    // 如果是FormData,不需要设置Content-Type
    if (is('FormData', data)) {
      header.delete('Content-Type');
      return request;
    }
    if (is('ArrayBuffer', data) ||
    is('File', data) ||
    is('Blob', data)) {
      return request;
    }
    // 部分浏览器内置URLSearchParams可以设置请求参数
    if (is('URLSearchParams', data)) {
      if (header &amp;&amp; !header.get('Content-Type')) {
        header.set('Content-Type', DEFAULT_TYPE);
      }
      request.setDataString(data.toString());
      return request;
    }

    if (is('Object', data)) {
      const tmp = JSON.stringify(data);
      if (header &amp;&amp; !header.get('Content-Type')) {
        if (isNoDataMethod(request.method)) {
          header.set('Content-Type', DEFAULT_TYPE);
        } else {
          header.set('Content-Type', JSON_TYPE);
        }
      }
      request.setDataString(tmp);
      return request;
    }

    if (header &amp;&amp; !header.get('Content-Type')) {
      header.set('Content-Type', DEFAULT_TYPE);
    }
    return request;
  },
  reject: error => Promise.reject(error),
};

export default contentInterceptor;
</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-Core.html">Core</a></li><li><a href="module-JSONP.html">JSONP</a></li><li><a href="module-Utils.html">Utils</a></li><li><a href="module-XHR.html">XHR</a></li></ul><h3>Classes</h3><ul><li><a href="Header.html">Header</a></li><li><a href="InterceptorFactory.html">InterceptorFactory</a></li><li><a href="module-Core-Q.html">Q</a></li><li><a href="Request.html">Request</a></li><li><a href="Response.html">Response</a></li></ul><h3>Namespaces</h3><ul><li><a href="q.html">q</a></li></ul><h3>Global</h3><ul><li><a href="global.html#contentInterceptor">contentInterceptor</a></li><li><a href="global.html#defaultOptions">defaultOptions</a></li><li><a href="global.html#xrfsInterceptor">xrfsInterceptor</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Tue Apr 18 2017 23:31:24 GMT+0800 (CST)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
